﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Deeper.Common;


namespace Deeper.DataAccess
{
	[Serializable]
	public class WhereClip : IClip
	{
		public static readonly WhereClip All = new WhereClip((string)null);
		private List<MyDbParams> plist = new List<MyDbParams>();
		private string whereString { set; get; }

		/// <summary>
		/// 返回参数列表
		/// </summary>
		internal MyDbParams[] Parameters
		{
			get
			{
				return plist.ToArray();
			}
		}

		/// <summary>
		/// 自定义一个Where条件
		/// </summary>
		/// <param name="where"></param>
		/// <param name="parameters"></param>
		public WhereClip(string where, params MyDbParams[] parameters)
		{
			this.whereString = where;
			if (parameters != null)
			{
				if (parameters.Length > 0)
				{
					this.plist.Clear();
					this.plist.AddRange(parameters);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="right"></param>
		/// <returns></returns>
		public static bool operator true(WhereClip right)
		{
			return false;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="right"></param>
		/// <returns></returns>
		public static bool operator false(WhereClip right)
		{
			return false;
		}

		/// <summary>
		/// 非
		/// </summary>
		/// <param name="where"></param>
		/// <returns></returns>
		public static WhereClip operator !(WhereClip where)
		{
			if (where.IsNull()) return null;

			return new WhereClip(" not " + where.ToString(), where.Parameters);
		}



		/// <summary>
		/// 与
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static WhereClip operator &(WhereClip leftWhere, WhereClip rightWhere)
		{
			WhereClip clip = All.BothClip(leftWhere, rightWhere) as WhereClip;
			if (clip.IsNotNull()) return clip;

			List<MyDbParams> list = new List<MyDbParams>();
			list.AddRange(leftWhere.Parameters);
			list.AddRange(rightWhere.Parameters);

			return new WhereClip(leftWhere.ToString() + " and " + rightWhere.ToString(), list.ToArray());
		}

		/// <summary>
		/// 或者
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static WhereClip operator |(WhereClip leftWhere, WhereClip rightWhere)
		{
			WhereClip clip = All.BothClip(leftWhere, rightWhere) as WhereClip;
			if (clip.IsNotNull()) return clip;

			List<MyDbParams> list = new List<MyDbParams>();
			list.AddRange(leftWhere.Parameters);
			list.AddRange(rightWhere.Parameters);

			return new WhereClip(leftWhere.ToString() + " or " + rightWhere.ToString(), list.ToArray());
		}

		/// <summary>
		/// 添加括号
		/// </summary>
		/// <param name="where"></param>
		/// <returns></returns>
		public WhereClip Bracket()
		{
			this.whereString = string.Format("({0})", this.whereString);
			return this;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return whereString;
		}

		public string ToFormatsString()
		{
			string where = whereString;
			for (int i = 0; i < this.Parameters.Length; i++)
				where = where.Replace(Parameters[i].ParameterName, string.Format("{{{0}}}", i));
			return where;
		}

		public object[] GetObjectParameters()
		{
			List<object> list = new List<object>();
			for (int i = 0; i < this.Parameters.Length; i++)
				list.Add(this.Parameters[i].Value);
			return list.ToArray();
		}

	}
}
